package com.lxblyw.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lxblyw.entity.Project;
import com.lxblyw.util.DBHelp;
import com.lxblyw.util.RowMapper;

public class ProjectDao {
	private DBHelp<Project> db = new DBHelp<Project>();
	
	public void save(Project project) {
		String sql = "INSERT INTO t_project(id,`name`,`desc`,createtime) VALUES(?,?,?,?)";
		db.executeSQL(sql, project.getId(),project.getName(),project.getDesc(),project.getCreatetime());
	}

	public List<Project> findAll(String employeeid) {
		String sql = "SELECT id,`name`,`desc`,createtime FROM t_project WHERE id IN (SELECT projectid FROM t_employee_project WHERE employeeid=?) ORDER BY createtime";
		return db.executeQueryForList(sql, new ProjectRowMapper(), employeeid);
	}
	
	private class ProjectRowMapper implements RowMapper<Project>{

		public Project mapperRow(ResultSet rs) throws SQLException {
			Project project = new Project();
			project.setId(rs.getString("id"));
			project.setName(rs.getString("name"));
			project.setDesc(rs.getString("desc"));
			project.setCreatetime(rs.getString("createtime"));
			
			return project;
		}
		
	}

	public Project findProjectById(String projectID) {
		String sql = "SELECT id,`name`,`desc`,createtime FROM t_project WHERE id=? ORDER BY createtime";
		return db.executeQueryForObject(sql, new ProjectRowMapper(), projectID);
	}
	
}
